/**
 * huize Service Inc
 * All Rights Reserved @2018
 */
package com.huize.zhike.framework.common.util;


import com.huize.ladon.apm.common.enums.DateTypeEnum;
import com.huize.zhike.framework.common.asserts.AssertExt;
import com.huize.zhike.framework.common.enumeration.StatTypeEnum;
import com.huize.zhike.framework.common.param.DateRangeParam;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * 描述:
 *
 * @author tianyuliang
 * @version $Id: DelayDateTrendUtils.java, v0.1 2021/3/8
 */
public class DateTrendUtils {


    public static DateRangeParam buildYearTrendParam(Date accessDate) {
        Date currentDate = DatetimeUtils.getPreDay(accessDate, 1);
        Date startDate = buildTrendStartDate(currentDate, DateTypeEnum.YEAR_TOTAL);
        DateRangeParam param = new DateRangeParam(startDate, currentDate, StatTypeEnum.DELAY);
        return param;
    }

    public static DateRangeParam buildMonthTrendParam(Date accessDate) {
        Date currentDate = DatetimeUtils.getPreDay(accessDate, 1);
        Date startDate = buildTrendStartDate(currentDate, DateTypeEnum.MONTH_TOTAL);
        DateRangeParam param = new DateRangeParam(startDate, currentDate, StatTypeEnum.DELAY);
        return param;
    }

    /**
     * 趋势图-日期范围 180天
     */
    public static DateRangeParam buildHalfYearTrendParam(Date accessDate) {
        Date currentDate = DatetimeUtils.getPreDay(accessDate, 1);
        Date startDate = buildTrendStartDate(currentDate, DateTypeEnum.HALF_YEAR);
        DateRangeParam param = new DateRangeParam(startDate, currentDate, StatTypeEnum.DELAY);
        return param;
    }

    /**
     * 趋势图-日期范围 60天
     */
    public static DateRangeParam buildSixtyTrendParam(Date accessDate) {
        Date currentDate = DatetimeUtils.getPreDay(accessDate, 1);
        Date startDate = buildTrendStartDate(currentDate, DateTypeEnum.SIXTY);
        DateRangeParam param = new DateRangeParam(startDate, currentDate, StatTypeEnum.DELAY);
        return param;
    }

    /**
     * 趋势图-连续日期 年累计
     */
    public static List<String> buildYearTrendSerialDate(Date accessDate) {
        Date currentDate = DatetimeUtils.getPreDay(accessDate, 1);
        Date startDate = buildTrendStartDate(currentDate, DateTypeEnum.YEAR_TOTAL);
        return buildTrendSerialDate(currentDate, startDate);
    }

    /**
     * 趋势图-连续日期 月累计
     */
    public static List<String> buildMonthTrendSerialDate(Date accessDate) {
        Date currentDate = DatetimeUtils.getPreDay(accessDate, 1);
        Date startDate = buildTrendStartDate(currentDate, DateTypeEnum.MONTH_TOTAL);
        List<String> serialDates = new ArrayList<>();
        String end = DatetimeUtils.formatExtDate(currentDate);
        String start = DatetimeUtils.formatExtDate(startDate);
        serialDates.add(start);

        AtomicInteger index = new AtomicInteger(1);
        int maxCount = 30; // 每月除去第一天后，后续最大是30天
        while (index.get() <= maxCount) {
            String flag = DatetimeUtils.formatExtDate(DatetimeUtils.getNextDay(startDate, index.get()));
            if (!serialDates.contains(flag)) {
                serialDates.add(flag);
            }
            index.getAndIncrement();
            if (flag.equals(end)) {
                break;
            }
        }
        return serialDates;
    }

    /**
     * 趋势图-连续日期 180天
     */
    public static List<String> buildHalfYearTrendSerialDate(Date accessDate) {
        Date currentDate = DatetimeUtils.getPreDay(accessDate, 1);
        Date startDate = buildTrendStartDate(currentDate, DateTypeEnum.HALF_YEAR);
        return buildTrendSerialDate(currentDate, startDate);
    }

    /**
     * 趋势图-连续日期 60天
     */
    public static List<String> buildSixtyTrendSerialDate(Date accessDate) {
        Date currentDate = DatetimeUtils.getPreDay(accessDate, 1);
        Date startDate = buildTrendStartDate(currentDate, DateTypeEnum.SIXTY);
        return buildTrendSerialDate(currentDate, startDate);
    }

    /**
     * 构建趋势图开始时间
     */
    private static Date buildTrendStartDate(Date currentDate, DateTypeEnum typeEnum) {
        AssertExt.assertDateTypeEnum(typeEnum);
        if (DateTypeEnum.isYear(typeEnum)) {
            return DatetimesUtils.getDateYearOne(currentDate);
        } else if (DateTypeEnum.isMonth(typeEnum)) {
            return DatetimesUtils.getDateMonthOne(currentDate);
        } else if (DateTypeEnum.isHalfYear(typeEnum)) {
            return DatetimesUtils.getDateHalfYearDay(currentDate);
        } else if (DateTypeEnum.isSixty(typeEnum)) {
            return DatetimesUtils.getDateSixtyDay(currentDate);
        } else {
            return DatetimeUtils.getPreDay(currentDate, 1);
        }
    }

    /**
     * 构建趋势图连续日期
     */
    private static List<String> buildTrendSerialDate(Date currentDate, Date startDate) {
        List<String> serialDates = new ArrayList<>();
        int diffCount = DatetimeUtils.diffDay(startDate, currentDate);
        for (int index = diffCount - 1; index >= 0; index--) {
            String serial = DatetimeUtils.formatExtDate(DatetimeUtils.getPreDay(currentDate, index));
            serialDates.add(serial);
        }
        return serialDates;
    }

    public static void main(String[] args) {
        List<String> serialDates = buildYearTrendSerialDate(new Date());
        System.out.println("\n\n" + "serial = " + BaseUtils.defaultJoin(serialDates));
        System.out.println("size = " + BaseUtils.size(serialDates));

        serialDates = buildMonthTrendSerialDate(new Date());
        System.out.println("\n\n" + "serial = " + BaseUtils.defaultJoin(serialDates));
        System.out.println("size = " + BaseUtils.size(serialDates));

        serialDates = buildHalfYearTrendSerialDate(new Date());
        System.out.println("\n\n" + "serial = " + BaseUtils.defaultJoin(serialDates));
        System.out.println("size = " + BaseUtils.size(serialDates));

        serialDates = buildSixtyTrendSerialDate(new Date());
        System.out.println("\n\n" + "serial = " + BaseUtils.defaultJoin(serialDates));
        System.out.println("size = " + BaseUtils.size(serialDates));
    }

}